﻿using System;
using System.Collections.Generic;

using System.Text;
using MSL.DataBase.Lib;
using System.Data;
using System.Data.SQLite;
using System.Data.Common;
using System.Collections;
using MSL.DataBase.SQL;

namespace MSL.DataBase.Core
{
    internal class SQLiteHelper : DbBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="conn">链接字符串</param>
        public SQLiteHelper(string conn)
            : base(DataBaseType.SQLite, conn, DBConfig.SQLite)
        {

        }
        public SQLiteHelper()
            : base(DataBaseType.SQLite, DBConfig.ConnString, DBConfig.SQLite)
        {

        }
        
        public override void AddReturnPara()
        {
            AddParameters("@TotalCount", null, DbType.Int32, -1, ParameterDirection.ReturnValue);
        }
        internal override void AddCustomePara(string paraName, ParaType paraType)
        {
            switch (paraType)
            {
                case ParaType.OutPut:
                case ParaType.result:
                    SQLiteParameter para = new SQLiteParameter();
                    para.ParameterName = paraName;
                    if (paraType == ParaType.OutPut)
                    {
                        para.Direction = ParameterDirection.Output;
                    }
                    else
                    {
                        para.Direction = ParameterDirection.ReturnValue;
                    }
                    Com.Parameters.Add(para);
                    break;
            }
        }
        public void AddParameters(string parameterName, object value, DbType dataType, int size)
        {
            AddParameters(parameterName, value, dataType, size, ParameterDirection.Input);
        }
        public override System.Data.Common.DbParameter GetNewParameter()
        {
            return new SQLiteParameter();
        }

        #region 分页查询

        public override DataTable Query(string tableName, string primaryKey, string showFields, int pageIndex, int pageSize, string whereStr, string sortBy, string groupBy, out int recordCount)
        {
            recordCount = 0;
            DataTable dt = null;
            Hashtable parameters = new Hashtable();

            string sql = SQLString.SQLitePage(pageIndex, pageSize, whereStr, tableName, showFields, sortBy, recordCount, true);
            recordCount = Convert.ToInt32(ExecScalar(sql, false));
            if (recordCount > 0 && (pageIndex - 1) * pageSize < recordCount)
            {
                sql = SQLString.SQLitePage(pageIndex, pageSize, whereStr, tableName, showFields, sortBy, recordCount, false);
                dt = Query(sql, false);
            }
            base._com.Parameters.Clear();
            return dt;
        }

        #endregion

        #region 数据库管理

        /// <summary>
        /// 还原数据库
        /// </summary>
        /// <param name="dbName">数据库名</param>
        /// <param name="backfile">备份文件</param>
        /// <returns>还原成功与否提示</returns>
        public override bool RestoreDatabase(string dbName, string backfile)
        {
            return false;
        }

        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="dbName">数据库名</param>
        /// <param name="dbFileName">备份文件</param>
        /// <returns>是否备份成功</returns>
        public override bool BackDataBase(string dbName, string dbFileName)
        {
            return false;
        }

        /// <summary>
        /// 新建数据库
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        public override bool CreateDataBase(string databaseName)
        {
            return true;
        }

        /// <summary>
        /// 删除数据库
        /// </summary>
        public override bool DropDataBase(string databaseName)
        {
            return false;
        }
        #endregion
    }
}
